package com.crm.system.service.impl.userForumManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.userForumManager.UserForum;
import com.crm.model.qo.userForumManager.UserForumQO;
import com.crm.model.vo.userForumManager.UserForumInfoVO;
import com.crm.model.vo.userForumManager.UserForumVO;
import com.crm.service.userForumManager.UserForumDbService;
import com.crm.system.service.userForumManager.UserForumService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/5/24 9:36
 */
@Service
public class UserForumServiceImpl implements UserForumService {

    @Autowired
    private UserForumDbService userForumDbService;

    /**
     * 分页查询论坛信息
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param userForumQO
     * @return
     */
    @Override
    public DhPageInfo<UserForumVO> selectUserForumListPage(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, UserForumQO userForumQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        userForumQO.setLoginAccountId(userAccount.getAccountId());
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }
        if (Objects.nonNull(userForumQO.getAccountId())){
            if (!userAccount.getAccountId().equals(userForumQO.getAccountId())){
                //userForumQO.setForumType(0);
            }
        }
        List<UserForumVO> forumList = userForumDbService.selectUserForumListPage(userForumQO, order.toString());
        PageInfo<UserForumVO> pageInfo = new PageInfo<UserForumVO>(forumList);
        return new DhPageInfo<UserForumVO>(pageInfo, forumList);
    }

    /**
     * 发布论坛
     * @param userForum
     */
    @Override
    public UserForumVO addUserForum(UserForum userForum) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        userForum.setUserForumId(CommonUtils.getGUID());
        userForum.setCreateTm(new Date());
        userForum.setAccountId(userAccount.getAccountId());
        userForum.setCreatePeopleId(userAccount.getAccountId());
        Integer index = userForum.getForumContent().indexOf("#");
        if (index != -1){
            //第二个#号出现的位置
            Integer index2 = userForum.getForumContent().indexOf("#",index+1);
            //两个#号中间为该帖子标题，标题字数不能超过20个
            if (index2 != -1 && (index2 - index) <= 21){
                //截取标题
                userForum.setForumTitle(userForum.getForumContent().substring(index+1,index2));
            }
        }
        userForumDbService.addUserForum(userForum);
        UserForumQO userForumQO = new UserForumQO();
        userForumQO.setUserForumId(userForum.getUserForumId());
        //userForumQO.setForumType(0);
        userForumQO.setLoginAccountId(userAccount.getAccountId());
        List<UserForumVO> list= userForumDbService.selectUserForumListPage(userForumQO,null);
        UserForumVO forumVO = new UserForumVO();
        if (list.size()>0){
            forumVO = list.get(0);
        }
        return forumVO;
    }

    /**
     * 当前登录用户的论坛信息
     * @return accountId用户id为null时获取当前登录人的信息
     */
    @Override
    public UserForumInfoVO selectLoginUserForumInfo(String accountId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        if (Objects.isNull(accountId)){
            accountId = userAccount.getAccountId();
        }
        UserForumInfoVO vo = userForumDbService.selectLoginUserForumInfo(accountId);
        return vo;
    }

    /**
     * 删除论坛
     * @param userForumId
     * @return
     */
    @Override
    public boolean delUserForum(String userForumId) {
        return userForumDbService.delUserForum(userForumId);
    }
}
